package com.shizhanzhe.gxb.my.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.base.UserBean;
import com.shizhanzhe.gxb.my.bean.MyTeam;
import com.shizhanzhe.gxb.url.Path;
import com.shizhanzhe.gxb.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.gxb.widget.CircleImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

/**
 * Created by YJ on 2019/1/8.
 */
public class MyTeamActivity extends SwipeBackActivity {
    @BindView(R.id.img)
    CircleImageView img;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.puid)
    TextView puid;
    @BindView(R.id.ll_tjr)
    LinearLayout llTjr;
    @BindView(R.id.totalnum)
    TextView totalnum;
    @BindView(R.id.zjnum)
    TextView zjnum;
    @BindView(R.id.jrnum)
    TextView jrnum;
    @BindView(R.id.zrnum)
    TextView zrnum;
    @BindView(R.id.onenum)
    TextView onenum;
    @BindView(R.id.twonum)
    TextView twonum;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team_my);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarView(R.id.bar_0_view)
                .init();
        myteam();
    }
    @OnClick(R.id.backarea)
    public void onViewClicked() {
        finish();
    }
    private void myteam(){
        OkHttpDownloadJsonUtil.downloadJson(this, Path.myTeam(UserBean.uid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)){
                    try{
                        MyTeam myTeam = new Gson().fromJson(json, MyTeam.class);
                        if (myTeam.getData() != null){
                            llTjr.setVisibility(View.VISIBLE);
                            Glide.with(MyTeamActivity.this).load(myTeam.getData().getAvatar()).apply(new RequestOptions().error(R.mipmap.head)).into(img);
                            name.setText(myTeam.getData().getUname());
                            if ("0".equals(myTeam.getData().getMobile()) || "".equals(myTeam.getData().getMobile())){
                                phone.setText("");
                            } else {
                                phone.setText(myTeam.getData().getMobile());
                            }
                            puid.setText("(邀请码："+myTeam.getData().getUid()+")");
                        }
                        totalnum.setText("总人数\n"+myTeam.getTongji().getTotalnum()+"人");
                        zjnum.setText("直属总监\n"+myTeam.getTongji().getZjnum()+"人");
                        jrnum.setText(myTeam.getTongji().getJrnum()+"人\n今日");
                        zrnum.setText(myTeam.getTongji().getZrnum()+"人\n昨日");
                        onenum.setText(myTeam.getTongji().getOnenum()+"人\n直属会员");
                        twonum.setText(myTeam.getTongji().getTwonum()+"人\n直属会员下级");
                    } catch (Exception e){
                        Toast.makeText(MyTeamActivity.this, "数据异常", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(MyTeamActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
